package com.ny.service.impl;

import com.ny.mapper.NewsMapper;
import com.ny.domain.NewsDO;
import com.ny.dto.NewsDTO;
import com.ny.dto.PageResult;
import com.ny.query.NewsQuery;
import com.ny.service.NewsService;
import java.lang.Boolean;
import java.lang.Override;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ny.exception.BusinessException;
import java.util.List;


/**
 * 健身资讯管理模块的实现
 *
 * @author MSI-NB
 * @date 2025-03-08 13:14:38
 */
@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Override
    public Boolean publishNews(NewsDTO newsDTO) {
        NewsDO newsDO = new NewsDO();
        newsDO.setTitle(newsDTO.getTitle());
        newsDO.setContent(newsDTO.getContent());
        newsDO.setPublishDate(newsDTO.getPublishDate());
        // 假设创建人固定为admin
        newsDO.setCreateBy("admin");
        // 假设创建时间为当前时间
        newsDO.setCreateTime(new java.util.Date());
        try {
            int result = newsMapper.insert(newsDO);
            return result > 0;
        } catch (Exception e) {
            throw new BusinessException("发布资讯失败");
        }
    }

    @Override
    public Boolean updateNews(NewsDTO newsDTO) {
        NewsDO newsDO = newsMapper.selectById(newsDTO.getNewsId());
        if (newsDO == null) {
            throw new BusinessException("资讯不存在，无法更新");
        }
        if (newsDTO.getTitle() != null) {
            newsDO.setTitle(newsDTO.getTitle());
        }
        if (newsDTO.getContent() != null) {
            newsDO.setContent(newsDTO.getContent());
        }
        if (newsDTO.getPublishDate() != null) {
            newsDO.setPublishDate(newsDTO.getPublishDate());
        }
        // 假设更新人固定为admin
        newsDO.setUpdateBy("admin");
        // 假设更新时间为当前时间
        newsDO.setUpdateTime(new java.util.Date());
        try {
            int result = newsMapper.updateById(newsDO);
            return result > 0;
        } catch (Exception e) {
            throw new BusinessException("更新资讯失败");
        }
    }

    @Override
    public Boolean deleteNews(NewsDTO newsDTO) {
        NewsDO newsDO = newsMapper.selectById(newsDTO.getNewsId());
        if (newsDO == null) {
            throw new BusinessException("资讯不存在，无法删除");
        }
        try {
            int result = newsMapper.deleteById(newsDTO.getNewsId());
            return result > 0;
        } catch (Exception e) {
            throw new BusinessException("删除资讯失败");
        }
    }

    @Override
    public PageResult<NewsDO> getAllNews(NewsQuery newsQuery) {
        try {
            List<NewsDO> newsList = newsMapper.selectAll(newsQuery.getPageIndex(), newsQuery.getPageSize());
            long totalRecords = newsMapper.countAll();
            long totalPages = totalRecords / newsQuery.getPageSize() + (totalRecords % newsQuery.getPageSize() > 0L ? 1L : 0L);
            PageResult<NewsDO> pageResult = new PageResult<NewsDO>(totalPages, newsQuery.getPageIndex(), newsQuery.getPageSize(),newsList);
            return pageResult;
        } catch (Exception e) {
            throw new BusinessException("获取资讯列表失败");
        }
    }

    @Override
    public NewsDO getNewsById(NewsQuery newsQuery) {
        NewsDO newsDO = newsMapper.selectById(newsQuery.getNewsId());
        if (newsDO == null) {
            throw new BusinessException("资讯不存在");
        }
        try {
            return newsDO;
        } catch (Exception e) {
            throw new BusinessException("获取资讯失败");
        }
    }
}
